#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "utils.h"
#include "parent.h"

#define parent_self(obj) cobject_self((obj), CCLASS_NAME(parent_s))

static inline void __parent_ctor(parent_s *self)
{
    base_ctor(&self->bobj);
    cobject_override(&self->bobj, base_get_val, parent_get_val);
    cobject_override(&self->bobj, base_set_val, parent_set_val);
    cobject_inherit(self, &self->bobj);
    self->b = 0;
}

static inline void __parent_dtor(parent_s *self)
{
    base_dtor(&self->bobj);
    self->b = 0;
}

parent_s *parent_new(void)
{
    parent_s *self = cobject_new(CCLASS_INFO(parent_s));
    if (self) __parent_ctor(self);
    return self;
}

void parent_del(parent_s **pself)
{
    assert(pself);
    assert(parent_type(*pself));

    if (!(*pself)) return;

    __parent_dtor(*pself);
    cobject_del(*pself);
    *pself = NULL;
}

int parent_ctor(parent_s *self)
{
    assert(self);

    cobject_ctor(self, CCLASS_INFO(parent_s));
    __parent_ctor(self);
    return 0;
}

void parent_dtor(parent_s *self)
{
    assert(self);
    assert(parent_type(self));

    __parent_dtor(self);
    cobject_dtor(self);
}

int parent_print(cobject obj)
{
    parent_s *self = parent_self(obj);

    assert(self);

    base_print(&self->bobj);
    debug("b=%d\n", self->b);
    return 0;
}

CVIRTUAL int parent_get_val(cobject obj)
{
    parent_s *self = parent_self(obj);

    assert(self);

    // 若为子类对象，执行子类对虚函数的重写
    if (self != obj && self->parent_get_val) return self->parent_get_val(obj); 
    return self->b;
}

CVIRTUAL int parent_set_val(cobject obj, int val)
{
    parent_s *self = parent_self(obj);

    // 若为子类对象，执行子类对虚函数的重写
    if (self != obj && self->parent_set_val) return self->parent_set_val(obj, val); 

    self->b = val;
    return 0;
}
